package cn.lds.j368.dsbank.resolver;

import cn.lds.j368.dsbank.exception.ServiceException;
import cn.lds.j368.dsbank.exception.ValidatorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 系统异常 异常处理器
 */
@ControllerAdvice  //标记该类为全局异常处理器
public class SysExceptionResolver {

    private final static Logger log = LoggerFactory.getLogger(SysExceptionResolver.class);

    ValidatorException validatorException;

    ServiceException serviceException;

    @ExceptionHandler
    public String exception(Exception e, Model model){

        e.printStackTrace();

        if (e instanceof ValidatorException){
            validatorException = (ValidatorException) e;
            model.addAttribute("errorMsg",validatorException.getMsg());
        }else if (e instanceof ServiceException){
            serviceException = (ServiceException) e;
            model.addAttribute("errorMsg",serviceException.getMsg());
        }else {
            serviceException = new ServiceException("系统升级中……");
            model.addAttribute("errorMsg",serviceException.getMsg());
        }


        return "errors";
    }


}
